#!user/bin/python
# -*- coding:UTF-8 -*-

import time
import mysql.connector
from weatherCrawler import WeatherCrawler
from searchTopCrawler import SearchTopCrawler
from ticketOfficeCrawler import TicketOfficeCrawler


def MAIN():
    print("开始部署爬虫组")
    print("Set mysql config:")
    user = input("User:")
    password = input("Password:")
    host = input("Host:")
    port = input("Port:")
    database = input("DataBase:")
    config = {
        'user': user,
        'password': password,
        'host': host,
        'port': port,
        'database': database,
        'charset': 'utf8'
    }
    print("Set static folder:")
    folder = input("folder:")
    try:
        conn = mysql.connector.connect(**config)
        cursor = conn.cursor()
        cursor.close()
        conn.close()
        print("mysql test successful!")
    except Exception as err:
        print(str(err))
        return
    isSet = input('Do you want to initialize the database? (Y/N)')
    while isSet != 'Y' and isSet != 'N' and isSet != 'y' and isSet != 'n':
        isSet = input('Do you want to initialize the database? (Y/N)')
    Weather_Crawler = WeatherCrawler(config)
    SearchTop_Crawler = SearchTopCrawler(config)
    TicketOffice_Crawler = TicketOfficeCrawler(config, folder)
    if isSet == 'Y' or isSet == 'y':
        Weather_Crawler.initDataBase()
        SearchTop_Crawler.initDataBase()
        TicketOffice_Crawler.initDataBase()
    while True:
        Weather_Crawler.updateInfo()
        SearchTop_Crawler.updataInfo()
        TicketOffice_Crawler.updateInfo()
        time.sleep(60*60*2)


if __name__ == '__main__':
    MAIN()
